package nlp;
/*
 * Logic extracted from 
 * http://opennlp.apache.org/documentation/1.5.2-incubating/manual/opennlp.html#tools.chunker
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.util.Span;
//JVM profiling imports
import com.bea.jvm.MethodProfileEntry;
import com.bea.jvm.NotAvailableException;
import com.bea.jvm.ProfilingSystem;
import com.bea.jvm.UnapplicableMethodException;
import java.lang.reflect.Method;
/**
 * @author Sebastian Suarez Benjumea ssuarezbe@unal.edu.co
 *
 */
public class SentenceDetection {
	private String pathmodelFile;
	private SentenceDetectorME sentenceDetector;
	//Attributes used for the Profiling
	private ProfilingSystem profiler;
	public MethodProfileEntry methodProfileEntry;
	public Method method;
	Class params[]={String.class};
	//Attributes used in the method "sentenceExtractor()"in order to avoid create them all the time
	String senteces[];
	//Attributes used in the method "sentencesPositionExtraction()"in order to avoid create them all the time
	Span sentPosistions[];
	
	/*
	 * @param File The configuration file that contain the model for the Sentence Segmentation process
	 */
	public SentenceDetection(String pathConfigFile,ProfilingSystem profiler){
		this.pathmodelFile=pathConfigFile+"en-sent.bin";
		// get Profiling System 
		this.profiler = profiler;
	}

	/*
	 * This method load the model in order to instantiate a SentenceDetectorME.
	 * It uses the File loaded in the constructor (attribute="modelFile") as the model file.
	 * Is should only call once in order to create only one "SentenceExtractorME". 
	 */
	public void init(){
		try{
		InputStream modelIn=new FileInputStream(this.pathmodelFile);
		SentenceModel model=new SentenceModel(modelIn);
		this.sentenceDetector=new SentenceDetectorME(model);
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		/*
		 * Definition of the JRockit profiling code lines
		 * For more info check:
		 * http://www.rgagnon.com/javadetails/java-0031.html
		 * http://www.tomecode.com/2010/10/02/profiling-java-ap-with-jmapi-jrockit/
		 */
		// get method append from StringBuffer class
		try {
			this.method = SentenceDetection.class.getDeclaredMethod("sentenceExtractor", this.params);
			// set method to profiler
			this.methodProfileEntry = profiler.newMethodProfileEntry(method);
			this.methodProfileEntry.setInvocationCountEnabled(true);
			this.methodProfileEntry.setTimingEnabled(true);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotAvailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnapplicableMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * This method receives a line and splits it into sentences and return an array containing the sentences.
	 *@param String  Line to be split.
	 *@return  String[] sentences array.
	 */
	public String[] sentenceExtractor(String fileLine){
		this.senteces=sentenceDetector.sentDetect(fileLine);
		return this.senteces;
	}
	
	/*
	 * This method receives a line and get the start/end position of each sentence in the line.
	 * @param String Line to be split.
	 * @return Span[] sentences start/end positions. 
	 */
	public Span[] sentencesPositionExtraction(String fileLine){
		this.sentPosistions=sentenceDetector.sentPosDetect(fileLine);
		return this.sentPosistions;
	}
}
